package com.wang.users.service.impl;

import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.pojo.JSONEnum;
import com.wang.common.service.impl.BaseServiceImpl;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAQuesDescDao;
import com.wang.users.dao.IAQuesDetailDao;
import com.wang.users.dao.IAUsersDao;
import com.wang.users.dao.impl.AQuesDescDaoImpl;
import com.wang.users.dao.impl.AQuesDetailDaoImpl;
import com.wang.users.dao.impl.AUsersDaoImpl;
import com.wang.users.pojo.AQuesDetail;
import com.wang.users.pojo.AQuesDetailEnum;
import com.wang.users.pojo.AQuesDesc;
import com.wang.users.pojo.AQuesDescEnum;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IQuesDbService;
import com.wang.users.service.IUsersDbService;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * usersService的实现类 Service需要通过读写器赋值
 *
 * @author wangshMac
 */
public class QuesDbServiceImpl extends BaseServiceImpl implements IQuesDbService {
    private IAQuesDescDao quesDescDao = new AQuesDescDaoImpl();
    private IAQuesDetailDao quesDetailDao = new AQuesDetailDaoImpl();
    private IAUsersDao usersDao = new AUsersDaoImpl();

    @Override
    public ApiResponse<Object> saveOneQuesDescService(AQuesDesc pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        pojo.setStatus(AQuesDescEnum.STATUS_ENABLE.getStatus());
        pojo.setCreateTime(new Date());
        pojo.setUpdateTime(new Date());
        pojo.setPubTime(new Date());
        int res = this.quesDescDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneQuesDescService(AQuesDesc pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        pojo.setUpdateTime(new Date());
        int res = this.quesDescDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneQuesDescService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.quesDescDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AQuesDesc> findCondListQuesDescService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AQuesDesc> apiResponse = new ApiResponse<AQuesDesc>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AQuesDesc> demoQuesDescList = Collections.EMPTY_LIST;

        Map<String, List<AQuesDesc>> dataList = new HashMap<String, List<AQuesDesc>>();
        demoQuesDescList = this.quesDescDao.findCondList(pageInfoUtil, condMap);
        /* 查询扩展信息 */
        Map<String,Object> paramsCondMap = new HashMap<>();
        for (AQuesDesc dataTemp : demoQuesDescList) {
            paramsCondMap.put("id", dataTemp.getUsersId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                dataTemp.setUsers(dbOneRes);
            }
        }
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoQuesDescList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoQuesDescList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AQuesDesc> findOneQuesDescService(Map<String, Object> condMap) {
        ApiResponse<AQuesDesc> apiResponse = new ApiResponse<AQuesDesc>();

        Map<String, AQuesDesc> data = new HashMap<String, AQuesDesc>();
        AQuesDesc pojo = this.quesDescDao.findOne(condMap);
        if (Objects.nonNull(pojo)) {
            /* 查询扩展信息 */
            Map<String, Object> paramsCondMap = new HashMap<>();
            paramsCondMap.put("id", pojo.getUsersId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                pojo.setUsers(dbOneRes);
            }
        }
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> saveOneQuesDetailService(AQuesDetail pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("usersId", pojo.getUsersId());
        paramsMap.put("quesId", pojo.getQuesId());
        ApiResponse<AQuesDetail> dbApiResponse = this.findCondListQuesDetailService(new PageInfoUtil(), paramsMap);
        List<AQuesDetail> dataListJava = dbApiResponse.getDataListJava();
        if (!dataListJava.isEmpty()) {
            apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }

        pojo.setStatus(AQuesDetailEnum.STATUS_ENABLE.getStatus());
        pojo.setCreateTime(new Date());
        pojo.setUpdateTime(new Date());
        pojo.setPubTime(new Date());
        int res = this.quesDetailDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneQuesDetailService(AQuesDetail pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        pojo.setUpdateTime(new Date());
        int res = this.quesDetailDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneQuesDetailService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.quesDetailDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AQuesDetail> findCondListQuesDetailService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AQuesDetail> apiResponse = new ApiResponse<AQuesDetail>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AQuesDetail> demoQuesDetailList = Collections.EMPTY_LIST;

        Map<String, List<AQuesDetail>> dataList = new HashMap<String, List<AQuesDetail>>();
        demoQuesDetailList = this.quesDetailDao.findCondList(pageInfoUtil, condMap);
        /* 查询扩展信息 */
        Map<String,Object> paramsCondMap = new HashMap<>();
        for (AQuesDetail dataTemp : demoQuesDetailList) {
            paramsCondMap.put("id", dataTemp.getUsersId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                dataTemp.setUsers(dbOneRes);
            }
            paramsCondMap.put("id", dataTemp.getQuesId());
            AQuesDesc dbQuesDesc = this.quesDescDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                dataTemp.setQues(dbQuesDesc);
            }
        }
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoQuesDetailList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoQuesDetailList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AQuesDetail> findOneQuesDetailService(Map<String, Object> condMap) {
        ApiResponse<AQuesDetail> apiResponse = new ApiResponse<AQuesDetail>();

        Map<String, AQuesDetail> data = new HashMap<String, AQuesDetail>();
        AQuesDetail pojo = this.quesDetailDao.findOne(condMap);
        /* 查询扩展信息 */
        Map<String,Object> paramsCondMap = new HashMap<>();
        if (Objects.nonNull(pojo)) {
            paramsCondMap.put("id", pojo.getUsersId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                pojo.setUsers(dbOneRes);
            }
            paramsCondMap.put("id", pojo.getQuesId());
            AQuesDesc dbQuesDesc = this.quesDescDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                pojo.setQues(dbQuesDesc);
            }
        }
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
